<?php
//生成数据结构对比数组
function update($row){
	    $row=trim($row);
		$update=array();
		$update['install']=$row;
		$temp=explode('(',$row);
	    $temp1=explode("`",$temp['0']);
	    $tablename=substr($temp1['1'],4);
	    $temp2=ltrim($row,$temp['0']);
	    $temp2=ltrim($temp2,'(');
	    $temp3=strrchr($temp2,')');
	    $temp2=rtrim($temp2,$temp3);
	    $temp2=explode(",\n",$temp2);
	    $fields=$indexs=array();
	    foreach($temp2 as $key => $value){
			$value=trim($value);
		    if(substr($value,0,1)=="`"){
			    $temp=explode('`',$value);
			    $name=$temp['1'];
			    $fields[$name]=$value;
		    }
		    else{
				$temp=explode('`',$value);
			    $name=$temp['1'];
				if(!empty($name)){
					$value=trim(str_replace(strrchr($value,'COMMENT'),'',$value));//去掉注释
					$value=trim(str_replace('USING BTREE','',$value));//去掉 USING BTREE
					$indexs[$name]=$value;
				}
		    }
	    }
	    
	    $update['name']=$tablename;
	    $update['fields']=$fields;
	    $update['indexs']=$indexs;
	return $update;
}
function update_sql($type,$sql,$table,$field=''){
	switch($type){
		case 'add_field':
			$temp=<<<TPL
if(!pdo_fieldexists('{$table}','{$field}')){
	pdo_query('{$sql}');
}

TPL;
			break;
		case 'change_field':
			$temp=<<<TPL
if(pdo_fieldexists('{$table}','{$field}')){
	pdo_query('{$sql}');
}

TPL;
			break;
		case 'add_table':
			$temp=<<<TPL
if(!pdo_tableexists('{$table}')){
	pdo_query('{$sql}');
}

TPL;
			break;
		case 'add_index':
			$temp=<<<TPL
if(!pdo_indexexists('{$table}','{$field}')){
	pdo_query('{$sql}');
}

TPL;
			break;
		case 'change_index':
			$temp=<<<TPL
if(pdo_indexexists('{$table}','{$field}')){
	pdo_query('{$sql}');
}

TPL;
			break;
	}
	return $temp;
}
?>